import { RouterAddressComponent } from "../../../../Model/Client/Demo/Router/RouterAddressComponent";
import { ObjectSystemAttribute, FriendOf, ExpandOf } from "../../../../../Decorator/Decorator";
import { Awake2System } from "../../../../../Core/Module/EventSystem/IAwakeSystem";
import { Log } from "../../../../../Core/Module/Log/Log";
import { TimerComponent } from "../../../../../Core/Module/Timer/TimerComponent";
import { RandomGenerator } from "../../../../../Core/Helper/RandomGenerator";


/**
 * 使用 Expand 扩展方法
 * 
 * 必须作出以下申明，才能调用对象的扩展方法
 */
declare module "../../../../Model/Client/Demo/Router/RouterAddressComponent" {

    interface RouterAddressComponent {
        Init();
        GetAllRouter();
        WaitTenMinGetAllRouter();
        GetAddress();
        GetRealmAddress(account: string);
    }

}

@ExpandOf(RouterAddressComponent)
export class RouterAddressComponentSystem {

    private static async Init(self: RouterAddressComponent) {
        // self.RouterManagerIPAddress = NetworkHelper.GetHostAddress(self.RouterManagerHost);
        // await self.GetAllRouter();
    }

    private static async GetAllRouter(self: RouterAddressComponent) {
        // const url = `http://${self.RouterManagerHost}:${self.RouterManagerPort}/get_router?v=${RandomGenerator.RandUInt32()}`;
        // Log.Debug(`start get router info: ${url}`);
        // const routerInfo = await HttpClientHelper.Get(url);
        // Log.Debug(`recv router info: ${routerInfo}`);
        // const httpGetRouterResponse = JsonHelper.FromJson<HttpGetRouterResponse>(routerInfo);
        // self.Info = httpGetRouterResponse;
        // Log.Debug(`start get router info finish: {JsonHelper.ToJson(httpGetRouterResponse)}`);

        // 打乱顺序
        // RandomGenerator.BreakRank(self.Info.Routers);

        // self.WaitTenMinGetAllRouter().Coroutine();
    }

    // 等10分钟再获取一次
    private static async WaitTenMinGetAllRouter(self: RouterAddressComponent) {
        await TimerComponent.Instance.WaitAsync(5 * 60 * 1000);
        if (self.IsDisposed) {
            return;
        }
        await self.GetAllRouter();
    }

    private static GetAddress(self: RouterAddressComponent) {
        // if (self.Info.Routers.length == 0) {
        //     return null;
        // }
        // const address = self.Info.Routers[self.RouterIndex++ % self.Info.Routers.length];
        // const ss = address.split(':');
        // const ipAddress = IPAddress.Parse(ss[0]);
        // if (self.RouterManagerIPAddress.AddressFamily == AddressFamily.InterNetworkV6) {
        //     ipAddress = ipAddress.MapToIPv6();
        // }
        // return new IPEndPoint(ipAddress, int.Parse(ss[1]));
    }

    private static GetRealmAddress(self: RouterAddressComponent, account: string) {
        // const v = account.Mode(self.Info.Realms.length);
        // const address = self.Info.Realms[v];
        // const ss = address.Split(':');
        // const ipAddress = IPAddress.Parse(ss[0]);
        // //if (self.IPAddress.AddressFamily == AddressFamily.InterNetworkV6)
        // //{ 
        // //    ipAddress = ipAddress.MapToIPv6();
        // //}
        // return new IPEndPoint(ipAddress, int.Parse(ss[1]));
    }

}

@FriendOf(RouterAddressComponent)
@ObjectSystemAttribute()
export class RouterAddressComponentAwake2System extends Awake2System<RouterAddressComponent, string, number> {

    Awake(self: RouterAddressComponent, address: string, port: number): void {
        self.RouterManagerHost = address;
        self.RouterManagerPort = port;
    }

}